package com.java2007.zhangyu.restaurant.controller.front;

import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.entity.CartItem;
import com.java2007.zhangyu.restaurant.entity.Result;
import com.java2007.zhangyu.restaurant.pojo.Admin;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.service.OrderService;
import com.java2007.zhangyu.restaurant.service.TableService;
import com.java2007.zhangyu.restaurant.service.impl.OrderServiceImpl;
import com.java2007.zhangyu.restaurant.service.impl.TableServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 结账控制器
 * @author zy
 * @Version
 * @date 2021/01/29
 */
@WebServlet("/pay")
public class PayController extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    private TableService tableService = new TableServiceImpl();

    /**
     * 结账
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String payMoney(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String oIdStr = request.getParameter("oId");
        String moneyStr = request.getParameter("money");

        Long oId = Long.parseLong(oIdStr);
        //修改订单状态为已支付
        orderService.updateOrderStatus(oId, 1);
        //更新餐桌的状态
        Integer tId = (int) request.getSession().getAttribute("tableId");

        if(tId != null) {
            DinnerTable table = new DinnerTable();
            table.setDtId(tId);
            table.setDtPresetTime(null);
            table.setDtState("0");
            tableService.update(table);
        }

        //结账完了，session中相关的订单数据应该清除
        request.getSession().removeAttribute("tId");
        request.getSession().removeAttribute("oId");
        request.getSession().removeAttribute("orderDetailList");
        request.getSession().removeAttribute("total_price");

        return ServletConstant.PRE_REDIRECT + "/front?method=queryTableByStatus";
    }

    /**
     * 支付
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String money(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String parameter = request.getParameter("orderId");
        Long orderId = Long.valueOf(parameter);
        orderService.updateOrderStatus(orderId,1);

        return ServletConstant.PRE_FORWARD + "/backendOrder?method=findOrderAll";
    }
}
